<template>
	<view class="container">
		<!-- =============================================钱包余额============================================= -->
		<view class="wallert">
			<!-- 定位背景图 -->
			<image class="bg_image" src="https://community.chuangxiangdianli.com/images/neighborimg/market/mine/wallert/wallert_bg.png" mode=""></image>
			<!-- 内容 -->
			<view class="content">
				<view class="go_tixian" @click="go_tixian">
					提现
				</view>
				<view class="title">可用余额</view>
				<view class="money">￥<text>{{wallert_info.flea_money}}</text></view>
				<view class="shouru">
					<view>
						<view>累计收入</view>
						<view>￥<text>{{wallert_info.amount_income}}</text></view>
					</view>
					<view>
						<view>累计支出</view>
						<view>￥<text>{{wallert_info.takeout_money}}</text></view>
					</view>
				</view>
				<view class="tixian">累计提现金额： ￥{{wallert_info.takeout_money}}</view>
			</view>
		</view>
		<!-- =============================================s收入/指出nav============================================= -->
		<view class="nav">
			<view @click="changeIndex(2)">
				<view>支出</view>
				<view v-show="navIndex == 2"><view></view></view>
			</view>
			<view @click="changeIndex(1)">
				<view>收入</view>
				<view v-show="navIndex == 1">
					<view></view>
				</view>
			</view>
		</view>
		<!-- =============================================收入和日期============================================= -->
		<view class="moneyAndDate">
			<view class="qian">
				<text v-if="navIndex==2" style="margin-right: 10rpx;">支出:</text>
				<text v-if="navIndex==1" style="margin-right: 10rpx;">收入:</text>
				<text v-if="navIndex==2">￥{{wallert_info.takeout_money}}</text>
				<text v-if="navIndex==1">￥{{wallert_info.amount_income}}</text>
			</view>
			<view class="shijian">
				<picker mode="date" :value="time" 
					:start="startDate" 
					:end="endDate" 
					fields="month"
					@change="bindDateChange">
					<view class="uni-input">{{time}}</view>
				</picker>
			</view>
		</view>
		<!-- =============================================收入/支出明细============================================= -->
		<scroll-view class="list" scroll-y="true" @scrolltolower="onBottom">
			<template v-for="(val,key) in list">
				<view class="list_item" :key="item.id" v-if="JSON.stringify(list)!='{}'">
					<view class="item_data">
						{{key}}
					</view>
					<template v-for="v in val">
						<view class="detail">
							<image src="https://community.chuangxiangdianli.com/images/neighborimg/market/mine/wallert/shou.png" mode=""></image>
							<view class="item_info">{{v.msg}}</view>
							<view class="item_price">￥{{v.money}}</view>
						</view>
					</template>
				</view>
			</template>
			<u-loadmore :status="status" />
		</scroll-view>
	</view>
</template>

<script>
	// 引入封装的请求
	import request from '@/utils/request.js'	
	export default{
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return{
				// 时间类型
				params: {
					year: true,
					month: true,
					day: false,
				},
				navIndex: 2,
				// 钱包信息
				wallert_info:{
					flea_money:0,
					amount_income:0,
					takeout_money:0
				},
				// 初始的数组
				origin_list:[],
				// 解析后的数据
				list:{},
				// 当前页码
				pageNo:1,
				// 当前的数据条数
				limit:10,
				// 是否正在请求数据
				is_loading:false,
				// 是否请求完数据
				is_bottom:false,
				// 底部正在加载中
				status: 'loadmore',
				// 当前的时间
				time:currentDate
			}
		},
		computed: {
			// 开始和结束时间
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		onShow() {
			this.get_wallert_info()
		},
		methods:{
			changeIndex(num) {
				if(this.navIndex == num) return
				this.navIndex = num
				this.is_loading = false
				this.pageNo = 1
				this.status = 'loadmore'
				if(num==2){
					this.expend()
				}else{
					this.income()
				}
			},
			// 前往提现 
			go_tixian(){
				uni.navigateTo({
					url:'/neighbor/market/mine/wallert/tixian'
				})
			},
			// 切换时间
			bindDateChange(e){
				// console.log(e)
				this.time = e.target.value
				if(this.navIndex==2){
					this.expend()
				}else{
					this.income()
				}
			},
			// 获取钱包信息
			get_wallert_info(){
				let that = this
				request.get('Mini/circle/flea/fleaWallet').then(res=>{
					if(res.code==200){
						that.wallert_info = res.data
						that.expend()
					}
				})
			},
			// 获取支出记录
			expend(turn){
				let that = this
				that.is_loading = true
				request.get('Mini/circle/flea/recordList',{
					type:that.navIndex,
					time:that.time,
					page:that.pageNo,
					limit:that.limit
				}).then(res => {
					console.log('首页数据', res)
					that.is_loading = false
					if (res.code==200) {
						if(turn){
							if(res.data.data.length>0){
								that.origin_list = [
									...list,
									...res.data.data
								]
							}
						}else{
							that.origin_list = res.data.data
						}
						that.list = that.parse_list(that.origin_list)
						let number = that.origin_list.length
						if(res.data.count==number){
							// that.is_boottom = true
							that.status = 'nomore';
						}
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(res => {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				})
			},
			// 获取收入记录
			income(turn){
				let that = this
				that.is_loading = true
				request.get('Mini/circle/flea/recordList',{
					type:that.navIndex,
					time:that.time,
					page:that.pageNo,
					limit:that.limit
				}).then(res => {
					// console.log('首页数据', res)
					that.is_loading = false
					if (res.code==200) {
						if(turn){
							if(res.data.data.length>0){
								that.origin_list = [
									...list,
									...res.data.data
								]
							}
						}else{
							that.origin_list = res.data.data
						}
						that.list = that.parse_list(that.origin_list)
						let number = that.origin_list.length
						if(res.data.count==number){
							that.status = 'nomore';
						}
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(res => {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				})
			},
			// 页面底部加载
			onBottom(){
				let that = this
				this.status = 'loading';
				// console.log(11111)
				if(that.status = 'nomore' || that.is_loading){
					return false
				}
				this.pageNo++
				setTimeout(() => {
					that.get_release_list(true)
				}, 1000)
			},
			// 根据时间处理成对应的格式
			parse_list(arr){
				// console.log(arr)
				let obj = {}
				arr.forEach(v=>{
					let time = v.created_at.substr(0,7)
					let date = new Date().getTime()
					let now_time = this.$u.timeFormat(this.timestamp, 'yyyy-mm');
					if(time == now_time){
						time = "今天"
					}
					console.log(time==now_time)
					// console.log(time)
					if(obj.hasOwnProperty(time)){
						obj[time].push(v)
					}else{
						obj[time] = []
						obj[time].push(v)
					}
				})
				// console.log(obj)
				return obj
			},
			// 初始化时间
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				
				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;;
				// day = day > 9 ? day : '0' + day;
				// return `${year}-${month}-${day}`;
				return `${year}-${month}`;
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container{
		overflow: hidden;
	}
	.wallert{
		position: relative;
		width: calc(100% - 30rpx);
		height: 338rpx;
		border-radius: 20rpx;
		margin-top: 15rpx;
		margin: 0 auto;
		box-sizing: border-box;
		.bg_image{
			position: absolute;
			left: 0;
			top: 0;
			width: 100%;
			height: 100%;
		}
		.content{
			position: absolute;
			left: 0;
			top: 0;
			width: 100%;
			height: 100%;
			.go_tixian{
				position: absolute;
				top:35rpx;
				right: 42rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				width: 116rpx;
				height: 50rpx;
				background: #FFFFFF;
				box-shadow: 0px 0px 7rpx 2rpx rgba(141, 141, 141, 0.29);
				border-radius: 25rpx;
				font-size: 30rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #FFAE00;
			}
			.title{
				width: 100%;
				padding-left: 43rpx;
				box-sizing: border-box;
				margin: 22rpx 0 10rpx 0;
				line-height: 28rpx;
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #FFFFFF;
			}
			.money{
				padding-left: 39rpx;
				font-size: 36rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #FFFFFF;
				&>text{
					font-size: 60rpx;
				}
			}
			.shouru{
				display: flex;
				flex-direction: row;
				justify-content: flex-start;
				padding-left: 39rpx;
				box-sizing: border-box;
				margin-top: 10rpx;
				&>view{
					margin-right: 50rpx;
					&>view:nth-child(1){
						font-size: 22rpx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #FFFFFF;
					}
					&>view:nth-child(2){
						margin-top: 10rpx;
						font-size: 24rpx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #FFFFFF;
						&>text{
							font-size: 30rpx;
						}
					}
				}
			}
			.tixian{
				width: 100%;
				margin-top: 25rpx;
				padding-left: 39rpx;
				box-sizing: border-box;
				font-size: 30rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #FFFFFF;
				background-color: rgba(242, 179, 36, 1);
			}
		}
	}
	.nav{
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		align-items: center;
		height: 107rpx;
		width: 100%;
		&>view{
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			&>view:nth-child(1){
				
				font-size: 30rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #333333;
			}
			&>view:nth-child(2){
				transition: all 0.1s linear 0s;
				position: relative;
				height: 3rpx;
				width: 78rpx;
				background-color: rgba(102, 102, 102, 1);
				&>view{
					position: absolute;
					left: 0;
					right: 0;
					bottom: -20rpx;
					margin: auto;
					width: 0;
					height: 0;
					border: 10rpx solid;
					border-color: transparent transparent rgba(102, 102, 102, 1);
					transform: rotate(180deg);
				}
			}
		}
	}
	.moneyAndDate{
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		height: 93rpx;
		width: 100%;
		padding: 0 68rpx;
		box-sizing: border-box;
		background-color: rgba(250, 250, 250, 1);
		.qian{
			font-size: 28rpx;
			font-family: Hiragino Sans GB;
			font-weight: bold;
			color: #0D85FF;
		}
		.shijian{
			
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #999999;
		}
		.activeColor{
			color: rgba(249, 167, 61, 1);
		}
	}
	.list{
		width: 100%;
		height: calc(100vh/2);
		padding: 0 30rpx;
		box-sizing: border-box;
		.list_item{
			border-bottom: 2rpx solid rgba(216, 213, 213, 1);
			.item_data{
				font-size: 24rpx;
				font-family: Hiragino Sans GB;
				font-weight: normal;
				color: #999999;
				margin-top: 25rpx;
			}
			.detail{
				display: flex;
				flex-direction: row;
				justify-content: flex-start;
				align-items: center;
				height: 130rpx;
				&>image{
					display: block;
					height: 51rpx;
					width: 51rpx;
				}
				.item_info{
					flex: 1;
					font-size: 30rpx;
					font-family: Hiragino Sans GB;
					font-weight: bold;
					color: #333333;
					margin: 0 43rpx;
				}
				.item_price{
					font-size: 28rpx;
					font-family: Hiragino Sans GB;
					font-weight: bold;
					color: #0D85FF;
				}
			}
		}
	}
</style>
